<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Question1
 *
 * @author Administrator
 */
require_once 'Question.php';
require_once 'Connection.php';

class Question1 extends Question{
    //put your code here

    public $title;
    public $answer1;
    public $answer2;
    public $answer3;
    public $answer4;
    public $answer5;

    public function   __construct() {
        $this->title = null;
        $this->answer1 = null;
        $this->answer2 = null;
        $this->answer3 = null;
        $this->answer4 = null;
        $this->answer5 = null;

    }

    public function  getQuestion($iNumQuestion=30) {
        $conn1 = new Connection();
        $conn = $conn1->open();

        $query = 'SELECT * FROM question WHERE id >= (SELECT FLOOR( MAX(id) * RAND()) FROM question ) ORDER BY id LIMIT '.$iNumQuestion;
        $result = mysql_query($query, $conn) or die("Could not connect").mysql_error();
        $questions = array();
        while($set = mysql_fetch_array($result, MYSQLI_ASSOC))
        {
            $temp = new Question1();
            $temp->id = $set['id'];
            $temp->type = $set['id_type'];
            $temp->title = $set['title'];
            $temp->answer1 = $set['answerA'];
            $temp->answer2 = $set['answerB'];
            $temp->answer3 = $set['answerC'];
            $temp->answer4 = $set['answerD'];
            $temp->answer5 = $set['answerOK'];
            $questions[] = $temp;
        }
        mysql_close($conn);
        return $questions;
    }

    public function getQuestionFromCalendar($groups=array(), $iNumQuestion=30){
        $i = ceil($iNumQuestion/(count($groups))); // Tinh so luong trung binh cua so luong tu vung o moi nguoi choi        
        $conn1 = new Connection();
        $conn = $conn1->open();
        $questions = array();
        for( $j = 0 ; $j < count($groups) ; ++$j){
            $query = 'SELECT * FROM saveword WHERE id_user = '.$groups[$j]->getName().' ORDER BY dateInput LIMIT '.$i;
            $result = mysql_query($query, $conn) or die("Could not connect").mysql_error();
            while($set = mysql_fetch_array($result, MYSQLI_ASSOC))
            {
                $temp = new Question1();
                $temp->id = $set['id'];
                $temp->type = 1;
                $temp->title = $set['vietnameseString'];
                $rand = rand(1, 4);
                $query1 = 'SELECT englishString FROM saveword WHERE id >= (SELECT FLOOR( MAX(id) * RAND()) FROM saveword ) ORDER BY dateInput ASC LIMIT 4';
                $result1 = mysql_query($query1, $conn);
                switch ($rand) {
                    case 1:
                        $temp->answer1 = $set['englishString'];

                        if( mysql_result($result1, 0) != $temp->answer1)
                            $temp->answer2 = mysql_result($result1, 0);
                        if( mysql_result($result1, 1) != $temp->answer1)
                            $temp->answer3 = mysql_result($result1, 1);
                        if( mysql_result($result1, 2) != $temp->answer1)
                            $temp->answer4 = mysql_result($result1, 2);

                        if( $temp->answer2 == null)
                            $temp->answer2 = mysql_result($result1, 3);
                        else if( $temp->answer3 == null)
                            $temp->answer3 = mysql_result($result1, 3);
                        else if( $temp->answer4 == null)
                            $temp->answer4 = mysql_result($result1, 3);
                        break;
                    case 2:
                        $temp->answer2 = $set['englishString'];

                        $result1 = mysql_query($query1, $conn);
                        if( mysql_result($result1, 0) != $temp->answer2)
                            $temp->answer1 = mysql_result($result1, 0);
                        if( mysql_result($result1, 1) != $temp->answer2)
                            $temp->answer3 = mysql_result($result1, 1);
                        if( mysql_result($result1, 2) != $temp->answer2)
                            $temp->answer4 = mysql_result($result1, 2);

                        if( $temp->answer1 == null)
                            $temp->answer1 = mysql_result($result1, 3);
                        else if( $temp->answer3 == null)
                            $temp->answer3 = mysql_result($result1, 3);
                        else if( $temp->answer4 == null)
                            $temp->answer4 = mysql_result($result1, 3);
                        break;
                    case 3:
                        $temp->answer3 = $set['englishString'];

                        $result1 = mysql_query($query1, $conn);
                        if( mysql_result($result1, 0) != $temp->answer3)
                            $temp->answer1 = mysql_result($result1, 0);
                        if( mysql_result($result1, 1) != $temp->answer3)
                            $temp->answer2 = mysql_result($result1, 1);
                        if( mysql_result($result1, 2) != $temp->answer3)
                            $temp->answer4 = mysql_result($result1, 2);

                        if( $temp->answer1 == null)
                            $temp->answer1 = mysql_result($result1, 3);
                        else if( $temp->answer2 == null)
                            $temp->answer2 = mysql_result($result1, 3);
                        else if( $temp->answer4 == null)
                            $temp->answer4 = mysql_result($result1, 3);
                        break;
                    case 4:
                        $temp->answer4 = $set['englishString'];

                        $result1 = mysql_query($query1, $conn);
                        if( mysql_result($result1, 0) != $temp->answer4)
                            $temp->answer1 = mysql_result($result1, 0);
                        if( mysql_result($result1, 1) != $temp->answer4)
                            $temp->answer2 = mysql_result($result1, 1);
                        if( mysql_result($result1, 2) != $temp->answer4)
                            $temp->answer3 = mysql_result($result1, 2);

                        if( $temp->answer1 == null)
                            $temp->answer1 = mysql_result($result1, 3);
                        else if( $temp->answer2 == null)
                            $temp->answer2 = mysql_result($result1, 3);
                        else if( $temp->answer3 == null)
                            $temp->answer3 = mysql_result($result1, 3);
                        break;
                    default:
                        break;
                }
                $temp->answer5 = $rand;
                $questions[] = $temp;
            }
        }
        mysql_close($conn);
        return $questions;
    }
}
?>
